package com.javasm.gg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.gg.PriceAdjustService;
import com.javasm.gg.PriceAdjust;
import com.javasm.gg.dao.PriceAdjustDao;
import com.javasm.process.ProcessService;
import com.javasm.process.entity.ProcessTask;
import com.javasm.process.entity.TaskStatus;
import com.javasm.sys.CurrentLogin;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class PriceAdjustServiceImpl extends ServiceImpl<PriceAdjustDao, PriceAdjust> implements PriceAdjustService {
    @Resource
    private ProcessService ps;
    private final String defName = "价格调整流程";

    @Override
    public List<ProcessTask> getTaskByApplyOrderId(String orderId) {
        return ps.getTaskByApplyOrderId(defName, orderId);
    }

    @Transactional
    @Override
    public boolean saveAndStart(PriceAdjust priceAdjust) {
        saveOrUpdate(priceAdjust);
        boolean b = ps.startProcess(defName, String.valueOf(priceAdjust.getId()));
        return b;
    }

    @Override
    public void approve(ProcessTask task) {
        String result = ps.approve(defName, task, CurrentLogin.getLoginUserName());
        if (null == result) return;

        //流程通过已结束或流程未通过
        PriceAdjust cgcp = new PriceAdjust();
        cgcp.setId(Integer.parseInt(task.getApplyId()));
        cgcp.setProcessStatus(result);
        updateById(cgcp);
    }

    @Override
    public List<ProcessTask> queryToDoTasks() {
        //查任务对象
        List<ProcessTask> taskList = ps.queryToDoTasks(defName,CurrentLogin.getLoginUserName());
        if(taskList.size()==0)return Collections.emptyList();
        return taskList;
    }

    @Override
    public List<ProcessTask> queryDosTasks(String approveResult) {
        //查任务对象 包括状态
        List<ProcessTask> taskList = ps.queryDosTasks(defName,CurrentLogin.getLoginUserName(),approveResult);
        if(taskList.size()==0)return Collections.emptyList();
        return taskList;
    }

    @Override
    public List<PriceAdjust> listPriceAdjuctAndGoods(PriceAdjust p) {
        return this.baseMapper.listPriceAdjuctAndGoods(p);
    }

    @Override
    public List<PriceAdjust> listPriceAdjuctAndGoodsByIds(List<String> applyIds) {
        return this.baseMapper.listPriceAdjuctAndGoodsByIds(applyIds);
    }

    @Override
    public boolean claim(String orderId) {
        Boolean claim = ps.claim(CurrentLogin.getLoginUserName(), orderId);
        return claim;
    }

    @Override
    public PriceAdjust getPriceAdjuctAndGoodsById(String id) {
        return this.baseMapper.getPriceAdjuctAndGoodsById(id);
    }
}
